﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Xml;
using DuAnNH.Fd_class;

namespace DuAnNH
{
    public partial class Frm_Login : Form
    {
        public Frm_Login()
        {
            InitializeComponent();
        }
        Cls_HeThong ht = new Cls_HeThong();
        private void Btn_DN_Click(object sender, EventArgs e)
        {
            string filePath = "Config.xml";
            if (!System.IO.File.Exists(filePath))
            {
                GhiCauHinh();
                DocCauHinh();
            }
            else if (System.IO.File.Exists(filePath))
            {
                DocCauHinh();
            }
        }
        public void LoadUser()
        {
            DataTable DT = new DataTable();
            MaHoa mh = new MaHoa();
            string pwd = mh.Mahoa(tbMatKhau.Text);
            DT = Coso.LoadUser(tbTenDN.Text, pwd);
            if (DT.Rows.Count == 1)
            {
                Hide();
                DataTable tb_Quyen;
                tb_Quyen = ht.get_chucnang_user(tbTenDN.Text, pwd);
                Frm_Main fm = new Frm_Main(tb_Quyen, tbTenDN.Text, pwd);
                fm.Show();
            }
            else
                MessageBox.Show("Lỗi đăng nhập! \n\n Vui lòng kiểm tra lại thông tin ^^!", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);

        }
        public void GhiCauHinh()
        {
            string filePath = "Config.xml";
            //System.IO.File.Create(filePath);
            System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create);
            fs.Close();
            Cls_XML clsxml = new Cls_XML();
            clsxml.LuuConXML();
        }
        public void DocCauHinh()
        {
            Cls_XML clsdoc = new Cls_XML();
            string file = "Config.xml";
            XmlDocument doc = new XmlDocument();
            doc.Load(file);
            XmlElement element = doc.DocumentElement;
            string sever = element.SelectSingleNode("sever").InnerText;
            string csdl = element.SelectSingleNode("database").InnerText;
            string user = element.SelectSingleNode("user").InnerText;
            string pass = element.SelectSingleNode("pass").InnerText;
            MaHoa mh = new MaHoa();
            string sv = mh.GiaiMa(sever);
            string data = mh.GiaiMa(csdl);
            string un = mh.GiaiMa(user);
            string pwd = mh.GiaiMa(pass);
            if (Coso.MoKetNoi(sv, data, un, pwd))
            {
                LoadUser();
            }
            else
            {
                if (MessageBox.Show("Lỗi kết nối cơ sở dữ liệu! \n\n Vui lòng cấu hình lại hệ thống!", "Cảnh Báo!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    Frm_CauHinh fch = new Frm_CauHinh();
                    fch.Show();
                }
            }
        }

        private void Btn_Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Frm_Login_Load(object sender, EventArgs e)
        {
            
        }
    }
}
